package com.yuedou.api.service.product.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuedou.api.dao.product.ProductAttrValueMapper;
import com.yuedou.api.dao.product.ProductCartMapper;
import com.yuedou.api.entity.product.Product;
import com.yuedou.api.entity.product.ProductAttrValue;
import com.yuedou.api.entity.product.ProductCart;
import com.yuedou.api.entity.dto.ProductCartDto;
import com.yuedou.api.entity.product.ProductType;
import com.yuedou.api.entity.wxuser.User;
import com.yuedou.api.exception.TransactionalException;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.product.ProductCartService;
import com.yuedou.api.service.product.ProductService;
import com.yuedou.api.service.product.ProductTypeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Service
public class ProductCartServiceImpl extends BaseServiceImpl<ProductCartMapper, ProductCart> implements ProductCartService {
    @Resource
    private ProductCartMapper productCartMapper;
    @Resource
    private ProductAttrValueMapper productAttrValueMapper;
    @Resource
    private ProductService productService;
    @Resource
    private ProductTypeService productTypeService;

    @Value("${web.imgPrefix}")
    private String imgPrefix;//图片地址

    @Override
    public List<ProductCart> selectByMerId(Integer user_id, Integer mer_id, Integer table_id,Integer checkState) {
        QueryWrapper<ProductCart> qw = new QueryWrapper<>();
        qw.eq("user_id",user_id);
        qw.eq("is_del",0);
        qw.eq("is_pay",0);
        qw.eq("is_new",0);
        if(mer_id > 0){
            qw.eq("mer_id",mer_id);
        }
        if(table_id > 0){
            qw.eq("table_id",table_id);
        }

        if(checkState != null){
            qw.eq("check_state",checkState);
        }
        qw.orderByAsc("id");
        List<ProductCart> list = resetCart(productCartMapper.selectList(qw));
        return list;
    }

    @Override
    public List<ProductCart> selectByTableId(Integer mer_id, Integer table_id, Integer checkState) {
        QueryWrapper<ProductCart> qw = new QueryWrapper<>();
        qw.eq("is_del",0);
        qw.eq("is_pay",0);
        qw.eq("is_new",0);
        qw.eq("mer_id",mer_id);
        qw.eq("table_id",table_id);
        if(checkState != null){
            qw.eq("check_state",checkState);
        }
        qw.orderByAsc("id");
        List<ProductCart> list = resetCart(productCartMapper.selectList(qw));
        return list;
    }

    @Override
    public List<ProductCart> selectByUserId(Integer user_id,Integer checkState) {
        QueryWrapper<ProductCart> qw = new QueryWrapper<>();
        qw.eq("is_del",0);
        qw.eq("is_pay",0);
        qw.eq("is_new",0);
        qw.eq("user_id",user_id);
        if(checkState != null){
            qw.eq("check_state",checkState);
        }
        qw.orderByAsc("id");
        List<ProductCart> list = resetCart(productCartMapper.selectList(qw));
        return list;
    }

    private List<ProductCart> resetCart(List<ProductCart> list){
        if(CollUtil.isNotEmpty(list)){
            list.forEach(productCart -> {
                Product product = productService.getProductInfoById(productCart.getProductId(),1);
                productCart.setProduct(product);
            });
        }
        return list;
    }

    @Override
    public List<ProductCartDto> selectAllMerByUserId(Integer user_id, Integer checkState) {
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        if(checkState!=null){
            map.put("check_state",checkState);
        }
        List<ProductCartDto> list = productCartMapper.getMerListByCart(map);
        if(CollUtil.isNotEmpty(list)){
            for (ProductCartDto cartDto : list){
                QueryWrapper<ProductCart> qw = new QueryWrapper<>();
                qw.eq("is_del",0);
                qw.eq("is_pay",0);
                qw.eq("is_new",0);
                qw.eq("mer_id",cartDto.getMerId());
                qw.eq("user_id",user_id);
                if(checkState != null){
                    qw.eq("check_state",checkState);
                }
                qw.orderByAsc("id");

                List<ProductCart> carts = resetCart(productCartMapper.selectList(qw));
                cartDto.setCarts(carts);
            }
        }
        return list;
    }

    @Override
    @Transactional
    public int changeState(Long user_id, ProductCart productCart, Integer checkState, int is_all) {
        int ret = 0;
        try{

            if(is_all == 0){//不是修改全部的状态
                productCart.setCheckState(checkState);
                ret = productCartMapper.updateById(productCart);
                if(ret <= 0){
                    throw new TransactionalException("productCartMapper updateById error");
                }
            }else{// 全选 / 全不选
                QueryWrapper<ProductCart> qw = new QueryWrapper<>();
                qw.eq("user_id",user_id);
                qw.eq("is_del",0);
                qw.eq("is_pay",0);
                qw.eq("is_new",0);
                List<ProductCart> list = productCartMapper.selectList(qw);
                for (ProductCart cart : list){
                    ret = productCartMapper.update(cart,new LambdaUpdateWrapper<ProductCart>()
                            .set(ProductCart::getCheckState, checkState).eq(ProductCart::getId,cart.getId())
                    );
                    if(ret <= 0){
                        throw new TransactionalException("productCartMapper update error");
                    }
                }
            }
            return 1;
        }catch (Exception e){
            throw new TransactionalException("product order insert error");
        }
    }
}
